package net.equasoft.ratingreminder.fragment;

import net.equasoft.ratingreminder.R;
import net.equasoft.ratingreminder.dialog.BasicDialogBuilder;
import net.equasoft.ratingreminder.dialog.ImageDialogBuilder;
import net.equasoft.ratingreminder.io.PrefsTools;
import net.equasoft.ratingreminder.type.RatingDialogType;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;

public class RatingReminderDialogFragment extends DialogFragment {

    private TextView askTextView;
    private String appName;
    private RatingDialogType dialogType;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        Dialog dialog = null;

        switch (dialogType) {
        case IMAGE_DIALOG:
            dialog =  new ImageDialogBuilder(getActivity()).create();
            break;

        case BASIC_DIALOG:
            dialog =  new BasicDialogBuilder(getActivity()).create();
            break;

        default:
            dialog =  new BasicDialogBuilder(getActivity()).create();
            break;
        }
        
        return dialog;
    }

    protected void goRating() {
        try {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse("market://details?id=" + getActivity().getPackageName()));
            startActivity(intent);
        } catch (ActivityNotFoundException e) {
            Toast.makeText(getActivity(), R.string.rating_store_not_found, Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        AlertDialog dialog = (AlertDialog) getDialog();
        
        if (appName != null){
            askTextView = (TextView) dialog.findViewById(R.id.askRatingText);
            askTextView.setText(getString(R.string.rating_ask, appName));
        }
        dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                PrefsTools.setBool(getActivity(), PrefsTools.PREFS_RATED, true);
                goRating();
                RatingReminderDialogFragment.this.getDialog().cancel();
            }
        });
        
        dialog.getButton(AlertDialog.BUTTON_NEGATIVE).setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View v) {
                RatingReminderDialogFragment.this.getDialog().cancel();
            }
        });
    }

    public String getAppName() {
        return appName;
    }

    public void setAppName(String appName) {
        this.appName = appName;
    }

    public RatingDialogType getDialogType() {
        return dialogType;
    }

    public void setDialogType(RatingDialogType dialogType) {
        this.dialogType = dialogType;
    }
}
